package cn.xinfei.xdecision.common.model.enginex.model;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Data
public class TerminationInfo {

    private NodeOutPut outPut;

    private List<SelectedRule> selectedRule;

    private Object conditions;

    private Map<String, Integer> fieldTypeMap;

    public List<NodeCondition> getJsonConditions() {
        if (conditions instanceof List) {
            return (List<NodeCondition>) conditions;
        }
        return null;
    }

    public String getScriptConditions() {
        if (conditions instanceof String) {
            return (String) conditions;
        }
        return null;
    }

    public List<NodeOutPut> getAllOutPuts() {
        List<NodeOutPut> allOutPuts = new ArrayList<>();
        if (outPut != null && StringUtils.isNotEmpty(outPut.getFieldCode())) {
            outPut.setTerminationFlag(true);
            allOutPuts.add(outPut);
        }
        if (outPut != null) {
            List<NodeOutPut> customerOutput = outPut.getResult();
            if (CollectionUtils.isNotEmpty(customerOutput)) {
                Set<NodeOutPut> collect = customerOutput.stream().filter(p -> p != null && StringUtils.isNotEmpty(p.getFieldCode())).collect(Collectors.toSet());
                allOutPuts.addAll(collect);
            }
        }
        return allOutPuts;
    }

    public NodeOutPut getTerminationResult() {
        return outPut;
    }
}
